<?php

namespace Superadmins\Controller;

/**
 * 用户编辑
 * @author Administrator
 *
 */
class UserController extends CommonController {

    /**
     *搜索用户
     */
    public function index() {
        $map = $this->searchMap();
        $Member = M('Member');
        $count = $Member->where($map)->count(); // 查询满足要求的总记录数
        $Page = new PageController($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $user = $Member->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('user', $user);
        $this->assign('page', $show); // 赋值分页输
        $this->display();
    }

    public function searchMap() {
        if (I('get.')) {
            if (I('get.teacher_type') == 0 || I('get.teacher_type') == 1) {
                $map['teacher_type'] = I('get.teacher_type');
            }
            if (I('get.nickname')) {
                $map['nickname'] = array('like', '%' . I('get.nickname') . '%');
            }

            return $map;
        }
    }

    /**
     * 新增用户页面
     */
    public function editor() {
        //编辑
        if (I('get.uid')) {
            $member = M("Member");
            $map['uid'] = I('get.uid');
            $info = $member->where($map)->find();
            $this->assign('info', $info);
            $School = M('School');
            $term['school_id'] = $info['school_id'];
            $School_data = $School->field('school_id,school_name')->where($term)->find();
            $this->assign('school_data', $School_data);
            $terms['id']=I('get.uid');
            $ucenter_member=M("UcenterMember");
            $member = $ucenter_member->field('username,email,mobile')->where($terms)->find();
            $this->assign('member', $member);
        }
        $this->assign('school_name', $this->getAttribute());
        $this->display();
    }

    /**
     * 获取学校相关属性
     */
    public function getAttribute() {
        $School = M('School');
        return $School->field('school_id,school_name')->select();
    }

    /**
     * 根据操作选择新增或删除
     */
    public function memberAction() { //编辑学校信息
        $member = M("Member");
        $auth = M('AuthGroupAccess');
        $data['uid']=I('post.uid');
        $ucenter_member=M("UcenterMember");
        $password=I('post.password');
        //判断是否修改密码
        if($password !=""){     
            $user['password']=md5($password);
        }
        //判断是否有uid 是否是修改或者添加
        if ($data['uid']>0) {
            $data['nickname']=I('post.nickname');
            $data['uid']=I('post.uid');
            $data['birthday']=I('post.birthday');
            $data['qq']=I('post.qq');
            $data['score']=I('post.score');
            $data['status']=I('post.status');
            $data['teacher_type']=I('post.teacher_type');
            $data['school_id']=I('post.school_id');
            $data['update_time']=time();
            //修改会员信息表
            if ($member->save($data)) {
                if ($data['teacher_type'] == 1) {
                    $info['group_id'] = 3;
                    $info['uid'] = $data['uid'];
                    $maps['uid']=$data['uid'];
                    $list=$auth->where($maps)->find();
                    if(!$list){
                       $auth->add($info); 
                    }
                    
                }else{
                    $maps['uid']=$data['uid'];
                    if($auth->where($maps)->find()){
                        $auth->where($maps)->delete();
                    }
                } 
                //
                $user['email']=I('post.email');
                $user['mobile']=I('post.mobile');
                $user['username']=I('post.nickname');
                $user['update_time']=time();
                $maps['id'] = I('post.uid');
                $ucenter_member->where($maps)->save($user);
            }
            $this->success('修改成功');
        } else { //新增学校信息
            
            $user['username']=$data['nickname'];
            $user['password']=md5($data['password']);
            $user['reg_time']=time();
            $user['status']=1;
            $user['id']=$uid;
            $user['email']=$data['email'];
            $user['mobile']=$data['mobile'];
            $uid = $ucenter_member->add($user);
            $data['uid']=$uid;
            $member->add($data);
            
            
            if ($uid > 0) {
                $info['uid'] = $uid;
                if ($data['teacher_type'] == 1) {
                    $info['group_id'] = 3;
                    $auth->add($info);
                } 
                $this->success('添加成功');
            }
        }
    }

    /**
     * AJAX方式删除用户
     */
    public function removeSchoolInfo() {
        $user = M('Member');
        $map['uid'] = I('post.uid');
        $user->where($map)->delete();
        $auth = M('AuthGroupAccess');
        $auth->where($map)->delete();
        $this->ajaxReturn(I('post.uid'));
    }

}
